package com.kjj.basic.controller;

import com.kjj.basic.service.IFastDfsService;
import com.kjj.basic.util.AjaxResult;
import com.kjj.basic.util.FastdfsUtil;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


@RestController
@RequestMapping("/fastDfs")
public class FastDfsController {
    @Autowired
    private IFastDfsService fastDfsService;

    @PostMapping
    public AjaxResult upload(MultipartFile file){
        try {
            byte[] bytes = file.getBytes();  //拿文件内容
            String originalFilename = file.getOriginalFilename();
            String extension = FilenameUtils.getExtension(originalFilename);
            String path =  fastDfsService.upload(bytes, extension);
            return AjaxResult.me().setData(path);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("上传失败" + e.getMessage());
        }
    }
    @DeleteMapping
    public AjaxResult delete(String path){
        try {
            System.out.println(path);
            fastDfsService.delete(path);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("上传失败" + e.getMessage());
        }
    }
}
